/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Material.h"
#include <string>
#include <Converter.h>
#include <xml/Saver.h>
#include <xml/Stream.h>
#include <safe_new.h>

namespace liba
{
namespace lib3d
{
namespace material
{

/*converter::EnumPair<char,Material::BlendFunc> Material::BlendFuncNames[] = 
{
	converter::MakeEnumPair("ZERO",Material::ZERO),
	converter::MakeEnumPair("ONE",Material::ONE),
	converter::MakeEnumPair("SRC_COLOR",Material::SRC_COLOR),
	converter::MakeEnumPair("ONE_MINUS_SRC_COLOR",Material::ONE_MINUS_SRC_COLOR),
	converter::MakeEnumPair("SRC_ALPHA",Material::SRC_ALPHA),
	converter::MakeEnumPair("ONE_MINUS_SRC_ALPHA",Material::ONE_MINUS_SRC_ALPHA),
	converter::MakeEnumPair("DEST_ALPHA",Material::DEST_ALPHA),
	converter::MakeEnumPair("ONE_MINUS_DEST_ALPHA",Material::ONE_MINUS_DEST_ALPHA),
	converter::MakeEnumPair("DEST_COLOR",Material::DEST_COLOR),
	converter::MakeEnumPair("ONE_MINUS_DEST_COLOR",Material::ONE_MINUS_DEST_COLOR),
	converter::MakeEnumPair("SRC_ALPHA_SAT",Material::SRC_ALPHA_SAT),
	0
};*/

Material::Material()
:	specular_power( 0 ),
///		double_sided( false ),
	transparency( 0 ),
///		source_blend( SRC_ALPHA ), 
///		dest_blend( ONE_MINUS_SRC_ALPHA ),
	diffuse( 1, 1, 1 ),
	reflection( 1, 1, 1 ),
//	reflectivity( 0 ),
	emissive( 1, 1, 1 ),
	specular( 1, 1, 1 )
{}

bool Material::on_attribute(xml::Provider * prov, const std::string & name, const std::string & value )
{
///	if( name == "SourceBlend" )
///		return converter::convert( value, &source_blend );
///	if( name == "DestBlend" )
///		return converter::convert( value, &dest_blend );
	if( name == "SpecularPower" )
		return converter::convert( value, &specular_power );
///	if( name == "DoubleSided" )
///		return converter::convert( value, &double_sided );
	if( name == "Diffuse" )
		return converter::convert( value, &diffuse );
	if( name == "Reflection" )
		return converter::convert( value, &reflection );
//	if( name == "Reflectivity" )
//		return converter::convert( value, &reflectivity );
	if( name == "Emissive" )
		return converter::convert( value, &emissive );
	if( name == "Specular" )
		return converter::convert( value, &specular );
	if( name == "Transparency" )
		return converter::convert( value, &transparency );

	//skip ambient
	if( name == "Ambient")
		return true;

	return false;
}

void Material::on_save_attributes( xml::SaverBase * saver )const
{
	if(specular_power != 0)
		saver->save_attribute("SpecularPower", specular_power);

	if(diffuse != Color(1,1,1))
		saver->save_attribute("Diffuse", diffuse);

	if(reflection != Color(1,1,1))
		saver->save_attribute("Reflection", reflection);

	if(emissive != Color(1,1,1))
		saver->save_attribute("Emissive", emissive);

	if(specular != Color(1,1,1))
		saver->save_attribute("Specular", specular);

	if(transparency != 0)
		saver->save_attribute("Transparency", transparency);
}

} // namespace material
} // namespace lib3d
} // namespace liba

